home *** CD-ROM | disk | FTP | other *** search
/ Joystick Magazine 1995 July & August / cd No4 joystick No62.iso / mac / pc / EMULATOR / PC64 / IMPORT.CPP < prev    next >
C/C++ Source or Header  |  1993-11-28  |  2KB  |  83 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <conio.h>
  5. #include <fcntl.h>
  6. #include <io.h>
  7. #include <sys\types.h>
  8. #include <sys\stat.h>
  9. #include <dos.h>
  10. #include <ctype.h>
  11.  
  12. typedef unsigned char byte;
  13. typedef unsigned short word;
  14. typedef unsigned int uint;
  15. typedef unsigned long dword;
  16.  
  17. struct {
  18.   char acTag[8];
  19.   char acName[17];
  20.   byte bRecord;
  21. } C64Header = {
  22.   "C64File"
  23. };
  24.  
  25. int main(int argc, char** argv) {
  26.   _find_t find;
  27.   word wFind = _dos_findfirst("*.T64", _A_NORMAL, &find);
  28.   if (wFind) {
  29.     printf("There are no C64S tape files (*.T64) in this directory.\n");
  30.   } else {
  31.     while (!wFind) {
  32.       char acNew[80];
  33.       int hIn, hOut, iSize;
  34.       word wStart;
  35.       printf("%12s ", find.name);
  36.       char* pcSource = find.name;
  37.       char* pcDest = acNew;
  38.       while (*pcSource != '.') {
  39.         if (isalnum(*pcSource)) {
  40.           *pcDest++ = *pcSource;
  41.         }
  42.         pcSource++;
  43.       }
  44.       strcpy(pcDest, ".P00");
  45.       if (!_access(acNew, 0)) {
  46.         printf("skipped (target exists)\n");
  47.         goto Next;
  48.       }
  49.       hIn = _open(find.name, _O_BINARY | _O_RDONLY);
  50.       if (!hIn) goto Error;
  51.       static char acBuffer[16384];
  52.       iSize = _read(hIn, acBuffer, 1024);
  53.       if (iSize == -1) goto Error;
  54.       if (iSize < 1024 || memcmp(acBuffer, "C64S tape file", 14)) {
  55.         printf("skipped (wrong format)\n");
  56.         goto Next;
  57.       }
  58.       hOut = _open(acNew, _O_BINARY | _O_CREAT | _O_TRUNC | _O_RDWR, _S_IREAD | _S_IWRITE);
  59.       if (!hOut) goto Error;
  60.       memset(C64Header.acName, 0, 16);
  61.       strcpy(C64Header.acName, acNew);
  62.       *(long*)strchr(C64Header.acName, '.') = 0;
  63.       if (_write(hOut, &C64Header, 26) != 26) goto Error;
  64.       wStart = *(word*)(acBuffer + 0x42);
  65.       if (_write(hOut, &wStart, 2) != 2) goto Error;
  66.       while (iSize) {
  67.         iSize = _read(hIn, acBuffer, 16384);
  68.         if (iSize == -1) goto Error;
  69.         if (_write(hOut, acBuffer, iSize) != iSize) goto Error;
  70.       }
  71.       if (_close(hOut)) goto Error;
  72.       if (_close(hIn)) goto Error;
  73.       printf("--> %s\n", acNew);
  74.     Next:
  75.       wFind = _dos_findnext(&find);
  76.     }
  77.   }
  78.   return 0;
  79. Error:
  80.   perror(NULL);
  81.   return 1;
  82. }
  83.